<template>
	<view class="verified">
		<text class="ischeck_text">审核状态：{{identityInfo.ischeck_text}}</text>
		
		<view class="suggestContent">
			<input
				type="text"
				:value="truename"
				placeholder="请输入姓名"
				placeholder-class="input-empty"
				maxlength="20"
				data-key="truename"
				@input="inputChange"
				style="height:32px"
			/>
		</view>
		<view class="suggestContent">
			<input
				type="text"
				:value="identityno"
				placeholder="请输入身份证号"
				placeholder-class="input-empty"
				maxlength="18"
				data-key="identityno"
				@input="inputChange"
				style="height:32px"
			/>
		</view>
		
		<view class="exit-button">
			<button class="exit-button-text" @click="toSubmit" :disabled="submiting">提交审核</button>
		</view>
		
		<text class="recharge-text">重要提醒：一个身份证号只能绑定一个账号</text>
		
		<!-- <view class="qrinfo">
			<text class="qrtext"  @click="navToHref">在线实名认证二维码</text>
			<image class="bg" :src="userInfo.verified_href || '/static/images/identityafter.png'"></image>
		</view> -->
		
	</view>
</template>

<script>
import { serveraddress } from '@/utils/serverUrl'
	
export default {
	data() {
		return {
			token: '',
			userInfo: {},
			identityInfo: {},
			
			imageList: [],
			truename: '',
			identityno: '',
			submiting: false
		};
	},
	onLoad() {
		
	},
	onShow() {
		this.token = this.$api.get('token');
		if (!this.token) {
			uni.navigateTo({
				url: '/pages/public/login'
			});
			return;
		}
		
		//获取用户信息
		this.getmedate();
		//获取用户提交的认证信息
		this.getidentityinfo();
	},
	navToHref: function() {
		let openUrl = 'http://freelance.youfupingtai.com/gotoSign.do';
		
		// #ifdef APP-PLUS
		plus.runtime.openURL(openUrl);
		// #endif
		
		// #ifdef H5
		windows.location.href = openUrl;
		// #endif
	},
	methods: {
		//获取用户信息
		async getmedate() {
			const param_userinfo = {
				token: this.token
			};
			console.log('User/userinfo', param_userinfo);
			await this.$http.request('User', 'userinfo', 'GET', param_userinfo, (res) => {
				if (res.status == 1) {
					if (res.data.error == 0) {
						this.userInfo = res.data.data;
					}
				}
			});
		},
		//获取用户提交的认证信息
		async getidentityinfo() {
			const param_identityinfo = {
				token: this.token
			};
			await this.$http.request('User', 'identitydetail', 'GET', param_identityinfo, (res) => {
				if (res.status == 1) {
					if (res.data.error == 0) {
						this.identityInfo = res.data.data;
						this.truename = res.data.data.truename;
						this.identityno = res.data.data.identityno;
					}
				}
			});
		},
		inputChange(e) {
			const key = e.currentTarget.dataset.key;
			this[key] = e.detail.value;
		},
		//选择身份证正面
		chooseImagefront: function(){
			uni.chooseImage({
				count: 1, //默认1
				sizeType: ['compressed'],  //original 原图，compressed 压缩图，默认二者都有
				sourceType: ['camera','album'], //album 从相册选图，camera 使用相机，默认二者都有
				success: (res) => {
					const images = res.tempFilePaths;
					this.uploadFiles(images);
				}
			});
		},
		//身份证正面上传图片
		async uploadFiles(images){
			this.imageList.push({
				filePath: images[0],
				progress: 0
			});
			uni.showLoading({
				title: '请稍后..',
				mask: true,
			});
			
			let uploadUrl = '';
			try{
				uploadUrl = await this.uploadImage(images[0]);
			}catch(err){
				console.log(err);
				return;
			}
			
			if(uploadUrl){
				this.imageList[this.imageList.length - 1].src = uploadUrl;
				this.identityfront = uploadUrl;
				uni.hideLoading();
				
			}else{
				//上传失败处理
				this.imageList.pop();
				uni.hideLoading();
				uni.showToast({
					title: '上传中出现问题，已终止上传',
					icon: 'none',
					mask: true,
					duration: 2000
				});
			}
		},
		
		//选择身份证反面
		chooseImageafter: function(){
			uni.chooseImage({
				count: 1, //默认1
				sizeType: ['compressed'],  //original 原图，compressed 压缩图，默认二者都有
				sourceType: ['camera','album'], //album 从相册选图，camera 使用相机，默认二者都有
				success: (res) => {
					const images1 = res.tempFilePaths;
					this.uploadFiles1(images1);
				}
			});
		},
		//身份证反面上传图片
		async uploadFiles1(images){
			this.imageList.push({
				filePath: images[0],
				progress: 0
			});
			uni.showLoading({
				title: '请稍后..',
				mask: true,
			});
			
			let uploadUrl = '';
			try{
				uploadUrl = await this.uploadImage(images[0]);
			}catch(err){
				console.log(err);
				return;
			}
			
			if(uploadUrl){
				this.imageList[this.imageList.length - 1].src = uploadUrl;
				this.identityafter = uploadUrl;
				uni.hideLoading();
				
			}else{
				//上传失败处理
				this.imageList.pop();
				uni.hideLoading();
				uni.showToast({
					title: '上传中出现问题，已终止上传',
					icon: 'none',
					mask: true,
					duration: 2000
				});
			}
		},
		
		//上传图片到服务器
		uploadImage: function(file){
			return new Promise((resolve, reject)=> {
				//发送给后端的附加参数
				const formData = {
					token: this.token
				};
				this.uploadTask = uni.uploadFile({
					url: serveraddress + "/User/uploadImage", 
					filePath: file,
					name: 'file',
					formData: formData,
					success(uploadFileResult){
						console.log(uploadFileResult);
						const uploadFileRes = JSON.parse(uploadFileResult.data) || {};
						if(uploadFileRes.error === 0 && uploadFileRes.data){
							console.log(uploadFileRes.data);
							resolve(uploadFileRes.data);
						}else{
							reject('接口返回错误');
						}
					}, 
					fail(){
						reject('网络链接错误');
					}
				});
				//上传进度
				this.uploadTask.onProgressUpdate((progressRes)=> {
					this.imageList[this.imageList.length - 1].progress = progressRes.progress;
				});
			});
		},
		
		//提交审核
		toSubmit() {
			this.submiting = true;
			const { truename, identityno } = this;
			
			/* 数据验证模块*/
			let regex_truename = /^[\u4e00-\u9fa5]{2,4}$/;
			if (!regex_truename.test(truename)) {
				this.$helper.toast('姓名格式错误', '', '/static/toast/fail-circle.png');
				this.submiting = false;
				return;
			}
			let regex = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
			if (!regex.test(identityno)) {
				this.$helper.toast('身份证号格式错误', '', '/static/toast/fail-circle.png');
				this.submiting = false;
				return;
			}
			// if (!identityfront) {
			// 	this.$helper.toast('身份证正面不能为空', '', '/static/toast/fail-circle.png');
			// 	this.submiting = false;
			// 	return;
			// }
			// if (!identityafter) {
			// 	this.$helper.toast('身份证反面不能为空', '', '/static/toast/fail-circle.png');
			// 	this.submiting = false;
			// 	return;
			// }

			const param = {
				token: this.token,
				truename: truename,
				identityno: identityno
			};
			
			this.$http.request('User', 'identityverify', 'POST', param, res => {
				this.submiting = false;
				if (res.status == 1) {
					if (res.data.error == 0) {
						// 认证提交成功
						this.$helper.toast('认证提交成功，等待审核', 'sucess');
						//返回前一页
						setTimeout(() => {
							uni.navigateBack(1);
						}, 200);
					}
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.verified {
	
	.ischeck_text{
		display: block;
		padding: 16upx 20upx;
		color: #fa436a;
		font-size: 34upx;
	}
	
	.suggestContent {
		border-radius: 15upx;
		padding: 2upx;
		margin: 20upx 20upx;
		background: #efefef;
		
		.bg {
			width: 100%;
			height: 400upx;
			filter: blur(1px);
			opacity: 0.7;
		}
	}
	
	.exit-button{
		margin-top: 30upx;
		
		.exit-button-text {
			height: 76upx;
			line-height: 76upx;
			margin: 0upx 20upx;
			border-radius: 10px;
			background: $uni-color-primary;
			color: #fff;
			font-size: $font-lg;
		}
	}
	
	.recharge-text {
		display: block;
		padding: 16upx 20upx;
		color: #fa436a;
		font-size: 24upx;
	}
	
	.qrinfo {
		padding: 10upx;
		margin: 20upx 20upx;
		background: #efefef;
		text-align: center;
		
		.qrtext{
			display: block;
			padding: 16upx 20upx;
			color: #fa436a;
			font-size: 34upx;
		}
		
		.bg {
			width: 400upx;
			height: 400upx;
			opacity: 0.7;
		}
	}

	
}
</style>
